﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Zwms.Client.Helper;
using Zwms.Data.CommonObject;
using Zwms.Data.BusinessObjects;
using Zwms.Client.CommonUI;

namespace Zwms.Client.InWms
{
    public partial class InPlanBillUC : BaseUserControl
    {
        private EnumOpenType _enumOpenType; 
        private string _sInPlanBillCode;

        public InPlanBillUC()
        {
            InitializeComponent();
            tbsWarehouse.SetEditState(false);
        }

        public InPlanBillUC(EnumOpenType enumopentype, string sInPlanBillCode)
        {
            InitializeComponent();
            _enumOpenType = enumopentype;
            _sInPlanBillCode = sInPlanBillCode;
            tbsWarehouse.SetEditState(false);
            InitControl();
        }

        #region 私有函数
        private void InitControl()
        {
            cmbIBType.Items.Clear();
            foreach (KeyValuePair<string, string> kvp in CommonDefine.DictIBType)
            {
                cmbIBType.Items.Add(kvp.Value);
            }
            if (this._enumOpenType == EnumOpenType.New)
            {
                tbInPlanBillCode.Text = _sInPlanBillCode;
            }
            else if (this._enumOpenType == EnumOpenType.Open)
            {
                LoadBill(_sInPlanBillCode);
            }
        }

        public void SetEditState(bool b)
        {
            cmbIBType.Enabled = b;
            dpuPlanReceiveDate.SetEditState(b);
            tbsSender.SetEditState(b);
            tbsReciever.SetEditState(b);
            tbDeliveryMan.Enabled = b;
            tbDeliverManPhone.Enabled = b;
            tbVehicle.Enabled = b;

            cmbIBType.BackColor = Color.White;
            dpuPlanReceiveDate.SetTextBGColor(Color.White);
            tbsSender.SetTextBGColor(Color.White);
            tbsReciever.SetTextBGColor(Color.White);
            tbDeliveryMan.BackColor = Color.White;
            tbDeliverManPhone.BackColor = Color.White;
            tbVehicle.BackColor = Color.White;
        }

        public void LoadBill(string sInPlanBillCode)
        {
            ServiceReferenceZwms.ServiceZwmsClient client = new ServiceReferenceZwms.ServiceZwmsClient();
            BilInPlanBill record = new BilInPlanBill();
            this._sInPlanBillCode = sInPlanBillCode;
            record.Id = sInPlanBillCode;
            record = client.QueryInPlanBill(record);

            tbInPlanBillCode.Text = record.Id;
            cmbIBType.SelectedIndex = cmbIBType.Items.IndexOf(CommonDefine.DictIBType[record.IBType]);
            tbBillStatus.Text = CommonDefine.DictIPBillStatus[record.BillStatus];
            dpuPlanReceiveDate.SetDate(CommonHelper.GetDatetimeFromString(record.PlanReceiveDate));
            tbsWarehouse.SetTextBoxContent(record.WarehouseName);
            tbsWarehouse.Tag = record.WarehouseCode;
            tbsSender.SetTextBoxContent(record.SenderName);
            tbsSender.Tag = record.SenderCode;
            tbsReciever.SetTextBoxContent(record.RecieverName);
            tbsReciever.Tag = record.RecieverCode;
            tbDeliveryMan.Text = record.DeliveryManName;
            tbDeliverManPhone.Text = record.DeliverManPhone;
            tbVehicle.Text = record.Vehicle;
        }

        public BilInPlanBill GetBill()
        {
            if (string.IsNullOrWhiteSpace(this._sInPlanBillCode))
            {
                return null;
            }

            bool tempb = true;
            if (cmbIBType.SelectedIndex == -1)
            {
                cmbIBType.BackColor = Color.Yellow;
                tempb = false;
            }

            if (tbsWarehouse.GetTextBoxContent() == "")
            {
                tbsWarehouse.SetTextBGColor(Color.Yellow);
                tempb = false;
            }

            if (!tempb)
            {
                return null;
            }

            BilInPlanBill record = new BilInPlanBill();

            record.Id = tbInPlanBillCode.Text;
            if (cmbIBType.SelectedIndex != -1)
            {
                string sIBTypeCode = CommonHelper.GetDictKeyByValue(CommonDefine.DictIBType, cmbIBType.SelectedItem.ToString());

                if (sIBTypeCode != null)
                {
                    record.IBType = sIBTypeCode;
                }
            }

            if (!string.IsNullOrWhiteSpace(tbBillStatus.Text))
            {
                string sBillStatus = CommonHelper.GetDictKeyByValue(CommonDefine.DictIPBillStatus, tbBillStatus.Text);

                if (sBillStatus != null)
                {
                    record.BillStatus = sBillStatus;
                }
            }

            if (dpuPlanReceiveDate.SelectDateTime != null)
            {
                record.PlanReceiveDate = dpuPlanReceiveDate.SelectDateTime;
            }

            record.WarehouseName = tbsWarehouse.GetTextBoxContent();
            record.WarehouseCode = tbsWarehouse.Tag.ToString();
            if (!string.IsNullOrWhiteSpace(tbsSender.GetTextBoxContent()))
            {
                record.SenderName = tbsSender.GetTextBoxContent();
                record.SenderCode = tbsSender.Tag.ToString();
            }
            if (!string.IsNullOrWhiteSpace(tbsReciever.GetTextBoxContent()))
            {
                record.RecieverName = tbsReciever.GetTextBoxContent();
                record.RecieverCode = tbsReciever.Tag.ToString();
            }
            
            record.DeliveryManName = tbDeliveryMan.Text;
            record.DeliverManPhone = tbDeliverManPhone.Text;
            record.Vehicle = tbVehicle.Text;

            //判断字段完整性
            return record;
            
        }

        public void Clear()
        {
            SetEditState(false);
            tbInPlanBillCode.Text = string.Empty;
            tbBillStatus.Text = string.Empty;
            cmbIBType.SelectedIndex = -1;
            dpuPlanReceiveDate.SetVoidDate();
            tbsSender.SetTextBoxContent(string.Empty);
            tbsReciever.SetTextBoxContent(string.Empty);
            tbsWarehouse.SetTextBoxContent(string.Empty);
            tbDeliveryMan.Text = string.Empty;
            tbDeliverManPhone.Text = string.Empty;
            tbVehicle.Text = string.Empty;
        }
        #endregion

        #region  父窗体函数
        private void tbInput_Enter(object sender, EventArgs e)
        {
            this.Control_Enter(sender, e);
        }

        private void tbInput_Leave(object sender, EventArgs e)
        {
            this.Control_Leave(sender, e);
        }
        #endregion

        private void tbsWarehouse_SelectButtonClick(object sender, EventArgs e)
        {
            InfoWms.WarehouseWnd uc = new InfoWms.WarehouseWnd();
            CommonUI.ContainerForm window = new CommonUI.ContainerForm(uc, "选择收货仓库");
            uc.SetQueryState();
            if (window.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                tbsWarehouse.SetTextBoxContent(uc.SelectedRecord.WarehouseName);
                tbsWarehouse.Tag = uc.SelectedRecord.Id;
            }
        }

        private void tbsSender_SelectButtonClick(object sender, EventArgs e)
        {
            InfoWms.CompanyWnd uc = new InfoWms.CompanyWnd();
            CommonUI.ContainerForm window = new CommonUI.ContainerForm(uc, "选择发货方");
            uc.SetQueryState();
            if (window.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                tbsSender.SetTextBoxContent(uc.SelectedRecord.CompanyName);
                tbsSender.Tag = uc.SelectedRecord.Id;
            }
        }

        private void tbsReciever_SelectButtonClick(object sender, EventArgs e)
        {
            InfoWms.CompanyWnd uc = new InfoWms.CompanyWnd();
            CommonUI.ContainerForm window = new CommonUI.ContainerForm(uc, "选择收货方");
            uc.SetQueryState();
            if (window.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                tbsReciever.SetTextBoxContent(uc.SelectedRecord.CompanyName);
                tbsReciever.Tag = uc.SelectedRecord.Id;
            }
        }
    }
}
